package com.ss.android.article.browser.search;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.a.a.e;
import com.a.a.o;
import com.a.a.q;
import com.a.a.r;
import com.a.a.s;
import com.ss.android.article.browser.R;
import com.ss.android.article.browser.database.BrowserHistory;
import com.ss.android.article.browser.database.b.HistoryManager;
import com.ss.android.article.browser.database.b.h;
import com.ss.android.article.browser.database.bookmark.m;
import com.ss.android.article.browser.preference.PreferenceManager;
import com.ss.android.article.browser.preference.PreferenceManager.Suggestion;
import com.ss.android.common.app.AbsApplication;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public final class SearchHistoryAdapter extends BaseAdapter implements Filterable {
    private static final o d = new q((byte) 0);
    public final List<BrowserHistory> mBrowserHistoryList;
    private final List e;
    private final List f;
    private final List g;
    private final Comparator h;
    private final List i;
    @NonNull
    private final Context j;
    public b$b b;
    public View mTvClearBrowserHistory;
    private m k;
    private PreferenceManager mPreferenceManager;
    private h m;
    private Application mApplication;
    private boolean o;
    private Suggestion mSuggestion;
    private Filter q;
    private View.OnClickListener r;

    public SearchHistoryAdapter(@NonNull Context context) {
        this.mBrowserHistoryList = new ArrayList(5);
        this.e = new ArrayList(5);
        this.f = new ArrayList(5);
        this.g = new ArrayList(5);
        this.h = new d((byte) 0);
        this.i = new ArrayList(5);
        this.o = true;
        this.r = new View.OnClickListener() {

            public final void onClick(View view) {
                if (SearchHistoryAdapter.this.b != null) {
                    Object tag = view.getTag();
                    if (tag instanceof Integer && mBrowserHistoryList.size() > ((Integer) tag).intValue()) {
                        BrowserHistory browserHistory = mBrowserHistoryList.get(((Integer) tag).intValue());
                        if (browserHistory != null) {
                            SearchHistoryAdapter.this.b.a(browserHistory.title);
                        }
                    }
                }
            }
        };
        this.mPreferenceManager = PreferenceManager.getInstance();
        this.k = com.ss.android.article.browser.database.bookmark.a.a();
        this.m = HistoryManager.getInstance();
        this.mApplication = AbsApplication.getApplication();
        this.j = context;
        this.o = false;
        this.mSuggestion = this.mPreferenceManager.getSuggestion();
        s v0 = this.k.b();
        ((e) v0).b = android.support.design.a.c();
        v0.a(new com.ss.android.article.browser.search.d(this));
        this.q = new a(this, this, this.m);
    }

    static s a(SearchHistoryAdapter arg2, String arg3) {
        return s.a(new com.ss.android.article.browser.search.m(arg2.mApplication, arg3));
    }

    static void a(SearchHistoryAdapter searchHistoryAdapter, List arg3, List arg4, List arg5) {
        s v0 = s.a(new g(searchHistoryAdapter, arg3, arg4, arg5));
        ((e) v0).b = SearchHistoryAdapter.d;
        ((e) v0).c = android.support.design.a.b();
        v0.a(new f(searchHistoryAdapter));
    }

    static s b(SearchHistoryAdapter arg1, String arg2) {
        return s.a(new com.ss.android.article.browser.search.h(arg1, arg2));
    }

    static List b(SearchHistoryAdapter arg1) {
        return arg1.mBrowserHistoryList;
    }

    static List c(SearchHistoryAdapter arg1) {
        return arg1.i;
    }

    static List d(SearchHistoryAdapter arg1) {
        return arg1.f;
    }

    static List e(SearchHistoryAdapter arg1) {
        return arg1.e;
    }

    static List f(SearchHistoryAdapter arg1) {
        return arg1.g;
    }

    static Comparator g(SearchHistoryAdapter arg1) {
        return arg1.h;
    }

    static void h(SearchHistoryAdapter arg2) {
        com.a.a.a v0 = com.a.a.a.a(new com.ss.android.article.browser.search.e(arg2));
        ((e) v0).b = SearchHistoryAdapter.d;
        ((e) v0).c = android.support.design.a.b();
        v0.a();
    }

    static View i(SearchHistoryAdapter arg1) {
        return arg1.mTvClearBrowserHistory;
    }

    static boolean j(SearchHistoryAdapter arg2) {
        boolean v0 = (arg2.o) || arg2.mSuggestion == Suggestion.SUGGESTION_NONE ? false : true;
        return v0;
    }

    public final void a(@NonNull List arg2) {
        synchronized (this) {
            this.mBrowserHistoryList.clear();
            this.mBrowserHistoryList.addAll(((Collection) arg2));
            this.notifyDataSetChanged();
        }
    }

    public final int getCount() {
        return this.mBrowserHistoryList.size();
    }

    @NonNull
    public final Filter getFilter() {
        return this.q;
    }

    @Nullable
    public final Object getItem(int arg2) {
        Object v0 = arg2 > this.mBrowserHistoryList.size() || arg2 < 0 ? null : this.mBrowserHistoryList.get(arg2);
        return v0;
    }

    public final long getItemId(int arg3) {
        return ((long) arg3);
    }

    @Nullable
    public final View getView(int arg5, @Nullable View arg6, ViewGroup arg7) {
        Object v1_1;
        if (arg6 == null) {
            arg6 = LayoutInflater.from(this.j).inflate(com.ss.android.article.browser.R.layout.j0, arg7, false);
            c v0 = new c(arg6);
            arg6.setTag(v0);
            v1_1 = v0;
        } else {
            v1_1 = arg6.getTag();
        }

        Object v0_1 = this.mBrowserHistoryList.get(arg5);
        ((c) v1_1).a.setText(((BrowserHistory) v0_1).title);
        ((c) v1_1).b.setText(((BrowserHistory) v0_1).url);
        ((c) v1_1).c.setTag(Integer.valueOf(arg5));
        ((c) v1_1).c.setOnClickListener(this.r);
        return arg6;
    }

    public interface b$b {
        void a(String arg1);
    }

    final static class a extends Filter {
        @NonNull
        protected final SearchHistoryAdapter b;
        @NonNull
        private final h c;
        public final SearchHistoryAdapter a;

        a(SearchHistoryAdapter arg1, @NonNull SearchHistoryAdapter arg2, @NonNull h arg3) {
            this.a = arg1;
            this.b = arg2;
            this.c = arg3;
        }

        static SearchHistoryAdapter a(SearchHistoryAdapter.a arg1) {
            return arg1.b;
        }

        @NonNull
        public final CharSequence convertResultToString(@NonNull Object arg2) {
            return ((BrowserHistory) arg2).url;
        }

        @NonNull
        protected final Filter.FilterResults performFiltering(@Nullable CharSequence arg7) {
            s v1_1;
            s v2;
            Filter.FilterResults v0 = new Filter.FilterResults();
            if (arg7 == null || arg7.length() == 0) {
                b.h(this.b);
                v1_1 = this.c.d();
                ((e) v1_1).b = android.support.design.a.c();
                ((e) v1_1).c = android.support.design.a.b();
                v1_1.a(new i(this));
                v0.count = 1;
            } else {
                String v1 = arg7.toString().toLowerCase(Locale.getDefault()).trim();
                if (b.j(this.b)) {
                    v2 = b.a(this.b, v1);
                    if (android.support.design.a.e == null) {
                        android.support.design.a.e = new r((byte) 0);
                    }

                    ((e) v2).b = android.support.design.a.e;
                    ((e) v2).c = android.support.design.a.b();
                    v2.a(new j(this));
                }

                v2 = b.b(this.b, v1);
                ((e) v2).b = android.support.design.a.c();
                ((e) v2).c = android.support.design.a.b();
                v2.a(new k(this));
                v1_1 = this.c.b(v1);
                ((e) v1_1).b = android.support.design.a.c();
                ((e) v1_1).c = android.support.design.a.b();
                v1_1.a(new l(this));
                v0.count = 1;
            }

            return v0;
        }

        protected final void publishResults(CharSequence arg3, Filter.FilterResults arg4) {
            b.a(this.b, null, null, null);
        }
    }

    final class c {
        @NonNull
        final TextView a;
        @NonNull
        final TextView b;
        @NonNull
        final View c;

        c(@NonNull View arg2) {
            this.a = arg2.findViewById(com.ss.android.article.browser.R.id.title);
            this.b = arg2.findViewById(com.ss.android.article.browser.R.id.ac4);
            arg2.findViewById(com.ss.android.article.browser.R.id.ac2);
            this.c = arg2.findViewById(com.ss.android.article.browser.R.id.ac3);
        }
    }

    final class d implements Comparator {
        d(byte arg1) {
            this();
        }

        private d() {

        }

        public final int compare(@NonNull Object arg5, @NonNull Object arg6) {
            int v3 = R.drawable.fc;
            int v0 = -1;
            if (((BrowserHistory) arg5).icon == ((BrowserHistory) arg6).icon) {
                v0 = 0;
            } else if (((BrowserHistory) arg5).icon != v3) {
                if (((BrowserHistory) arg6).icon != v3 && ((BrowserHistory) arg5).icon == R.drawable.fg) {
                    return v0;
                }
                v0 = 1;
            }

            return v0;
        }
    }
}

